from selenium import webdriver
# 规避被检测
from selenium.webdriver import ChromeOptions
from selenium.webdriver.common.by import By

import time

option = ChromeOptions()
# # 可防止selenium被检测到合并了
# # ----------------------------------------------------
option.add_experimental_option('excludeSwitches', ['enable-automation'])
# "--headless"不跳出浏览器
# option.add_argument('--headless')
option.add_argument('--disable-gpu')
# #----------------------------------------------------
# #如何让selenium规避掉可检测到的风险
chrome = webdriver.Chrome(options=option)
chrome.get("https://mail.qq.com/")

iframe1 = chrome.find_element(By.XPATH, '//*[@id="QQMailSdkTool_login_loginBox_qq"]/iframe')
chrome.switch_to.frame(iframe1)

iframe2 = chrome.find_element(
    By.XPATH, '//*[@id="ptlogin_iframe"]')
chrome.switch_to.frame(iframe2)

# 使用find_element_by_id来定位<img>元素
img_element = chrome.find_element(By.ID, 'qrlogin_img')

# 获取src属性的值
src_value = img_element.get_attribute('src')

# 打印src属性的值
print(src_value)

def countdown(t):
    while t:
        mins, secs = divmod(t, 60)
        timer = '{:02d}:{:02d}'.format(mins, secs)
        print(timer, end="\r")  # 使用\r回到行首，覆盖之前的输出
        time.sleep(1)
        t -= 1
    print("时间到！")


# 示例：倒计时10秒
countdown(25)

chrome.find_element(By.XPATH, '//*[@id="composebtn_td"]').click()


def QQSendPrepare(address, title=""):
    chrome.find_element(By.XPATH, '//*[@id="composebtn_td"]').click()
    # 与之前相同，还是先转换到写信的frame
    iframe3 = chrome.find_element(By.ID, 'mainFrame')
    chrome.switch_to.frame(iframe3)
    # 输入收件人
    chrome.find_element(
        By.XPATH, '/html/body/form[2]/div[2]/div[3]/div[2]/table[2]/tbody/tr/td[2]/div[1]/div[2]/input').send_keys(address)
    # 输入主题
    chrome.find_element(
        By.XPATH, '/html/body/form[2]/div[2]/div[3]/table[3]/tbody/tr[2]/td[2]/div/div/div/input').send_keys(title)


def QQSend(address, text, title=""):
    QQSendPrepare(address, title)
    iframe4 = chrome.find_element(By.CLASS_NAME, 'qmEditorIfrmEditArea')
    chrome.switch_to.frame(iframe4)
    # 定位到文本框，并输入正文
    text_label = chrome.find_element(By.XPATH, '/html/body/div[1]')
    text_label.clear()
    text_label.send_keys(text)
    


if __name__ == '__main__':
    # 目标邮箱、正文、标题
    addressee = "573561243@qq.com"
    text = "This is a test email..."
    title = "test email"
    # 将浏览器网页最大化
    chrome.maximize_window()
    QQSend(address=addressee, text=text, title=title)






# iframe1 = chrome.find_element(
#     By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]/div/div[2]/div[2]/iframe')
# chrome.switch_to.frame(iframe1)
# iframe2 = chrome.find_element(
#     By.XPATH, '//*[@id="ptlogin_iframe"]')
# chrome.switch_to.frame(iframe2)
# switcher_plogin = chrome.find_element(By.ID, "switcher_plogin")
# switcher_plogin.click()
# user_name = chrome.find_element(By.ID, "u")
# user_name.send_keys('573561243')
# user_password = chrome.find_element(By.ID, "p")
# user_password.send_keys('Ldx12138')
# # login_button = chrome.find_element(
# #     By.XPATH, '/html/body/div[1]/div[5]/div/div[1]/div[3]/form/div[4]/a/input').click()
# chrome.find_element(By.XPATH, '//*[@id="login_button"]').click()
# # 等待30s直到写信的按钮出现
# # time.sleep(10)
# windows = chrome.window_handles
# # 切换到当前最新打开的窗口中
# chrome.switch_to.window(windows[-1])
# tcaptcha_iframe_dy = chrome.find_element(
#     By.XPATH, '/html/body/div[1]/div[12]/div[2]/div/iframe')
# chrome.switch_to.frame(tcaptcha_iframe_dy)
# tcaptcha_iframe_dy = chrome.find_element(By.XPATH, '//*[@id="embedAccessibilityButton"]/button')
# tcaptcha_iframe_dy.click()
countdown(2000)
